package practice_2025_9.practice_9_22;

class Solution {
    /**
     * 买卖股票的最佳时机II
     * @param prices
     * @return
     */
    public int maxProfit(int[] prices) {
        // 最多持有一股
        // 可以多次买卖 -> 只要有赚就买卖
        int res = 0;
        for(int i = 0; i < prices.length - 1; i++) {
            if (prices[i] < prices[i + 1]) {
                res += prices[i + 1] - prices[i];
            }
        }
        return res;
    }

    /**
     * 寻找旋转排序数组中的最小值
     * @param nums
     * @return
     */
    public int findMin(int[] nums) {
        // 二分
        int left = 0, right = nums.length - 1;
        while(left < right) {
            int mid = (left + right) / 2;
            // System.out.println("left: " + left + " mid: " + mid);
            if (nums[mid] > nums[right]) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return nums[left];
    }
}